
import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:provider/provider.dart';
import 'package:s_mankey/config/resource_mananger.dart';
import 'package:s_mankey/model/audio_detail_model.dart';
import 'package:s_mankey/provider/provider_widget.dart';
import 'package:s_mankey/ui/widget/loading_widget.dart';
import 'package:s_mankey/utils/audio_playes.dart';
import 'package:s_mankey/view_model/audio_vmodel.dart';

class AudioDetail extends StatefulWidget {
  final arguments;

  AudioDetail({Key key, this.arguments}) : super(key: key);

  @override
  _AudioDetailState createState() {
    return _AudioDetailState();
  }
}

class _AudioDetailState extends State<AudioDetail> {
  ScrollController _controller;

  @override
  void initState() {
    _controller = ScrollController();
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
//    AudioDetailVModel
    return  ProviderWidget<AudioDetailVModel>(
        model: AudioDetailVModel(vid: widget.arguments['vid']),
        onModelReady: (model) => model.initData(),
        builder: (context, model, child) {
          AudioDetailModel datas = model.datas;
          var audioModel = Provider.of<CMMAudioPlayer>(context);
          return
            (datas == null)? LoadingWidget() :
            Scaffold(
      appBar: AppBar(
        elevation: 1,
        title: Text(datas.now.courseTitle),
      ),
      bottomNavigationBar: ButtonBarWidget(datas),
      body: SingleChildScrollView(
        child:Column(
              children: [
                Column(
                  children: [
                    Padding(
                      padding: const EdgeInsets.only(top: 20),
                      child: Image.network(
                        datas.now.headImg,
                        width: 98,
                        height: 128,
                        fit: BoxFit.fill,
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.all(30),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [

                          Padding(
                            padding: const EdgeInsets.only(right: 30),
                            child: Icon(
                              Icons.navigate_before,
                              size: AppFontSize.Size28,
                            ),
                          ),

//                          Consumer<CMMAudioPlayer>(builder: (context, model ,child){
//                            return Text(model.curTime.toString());
//                          }),

//                          GestureDetector(onTap: (){
//                            print('播放');
//                            cmmPlayer.play(datas.now.audioUrl);
//                            cmmPlayer.isPlayer = true;
//                          }, child:Icon(
//                            Icons.play_circle_filled,
//                            size: AppFontSize.Size28,
//                          )),

                          Offstage(offstage: audioModel.isPlayer ,child: GestureDetector(onTap: (){
                            cmmPlayer.play(datas.now.audioUrl);
                            audioModel.setIsPlayer(true);
                          }, child:Icon(
                            Icons.play_circle_filled,
                            size: AppFontSize.Size28,
                          )),),

                          Offstage(offstage: !audioModel.isPlayer ,child: GestureDetector(onTap: (){
                            cmmPlayer.pause();
                            audioModel.setIsPlayer(false);
                          }, child:Icon(
                            Icons.pause,
                            size: AppFontSize.Size28,
                          )),),


//                          GestureDetector(onTap: (){
//                            print('继续播放');
//                            cmmPlayer.resume();
//                          }, child: Icon(
//                            Icons.redo,
//                            size: AppFontSize.Size28,
//                          ),),
//
//                          GestureDetector(onTap: (){
//                            print('停止');
//                            cmmPlayer.stop();
//                          }, child: Icon(
//                            Icons.stop,
//                            size: AppFontSize.Size28,
//                          ),),
//
//                          GestureDetector(onTap: (){
//                            print('暂停');
//                            cmmPlayer.pause();
//                          }, child: Icon(
//                            Icons.pause,
//                            size: AppFontSize.Size28,
//                          ),),
//
//                          GestureDetector(onTap: (){
//                            print('跳时间');
//                            cmmPlayer.seek(time: 2000);
//                          }, child: Icon(
//                            Icons.timer,
//                            size: AppFontSize.Size28,
//                          ),),

                          Padding(
                            padding: const EdgeInsets.only(left: 30),
                            child: Icon(
                              Icons.chevron_right,
                              size: AppFontSize.Size28,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(bottom: 10),
                      width: AppCustomSize.ScreentWidght,
                      height: 10,
                      color: AppColor.dividerColor,
                    ),
//                    Container(
//                      padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
//                      child: Image.network(
//                        model.datas['now']['head_img'],
//                        fit: BoxFit.fill,
//                      ),
//                    ),

                    Padding(
                      padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
                      child:Html(data: datas.now.soundContent,),
                    ),
                  ],
                ),
              ],
            ),
      ),);
    });
  }

  Widget ButtonBarWidget(AudioDetailModel datas) {
    print('我的$datas');
    return   Container(
      padding: EdgeInsets.fromLTRB(30, 20, 30, 0),
      margin: EdgeInsets.only(bottom: 20),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              Icon(Icons.dehaze),
              Padding(
                padding: const EdgeInsets.only(left: 2),
                child: Text(
                  datas.now.player.toString(),
                ),
              ),
            ],
          ),
          Row(
            children: [
              Icon(
                Icons.favorite,
                color: Colors.yellow,
              ),
              Padding(
                padding: const EdgeInsets.only(left: 2),
                child:
                Text(
                  datas.now.collect.toString(),
                ),
              ),
            ],
          ),
          Row(
            children: [
              Icon(Icons.whatshot),
              Padding(
                padding: const EdgeInsets.only(left: 2),
                child: Text((
                datas.now.like.toString())
                ),
              ),
            ],
          ),
        ],
      ),
    );


  }
}
